package helpers;

import java.net.URL;
import java.util.TreeMap;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class CrawlerHelper {
	public static String getWhois(String name) {
		
		String url = "http://www.whois.com/whois/" + name;
		String whois = "";
		try {
			Connection conn = Jsoup.connect(url);
			Document doc = conn
					.header("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
					.header("accept-Language", "en-us,en;q=0.5")
					.header("host", "whoisainfo.net")
					.timeout(3000)
					.get();
					
					
			
			
			whois = doc.select(".whois_result").html();
			
			if (!whois.isEmpty()) {
				whois = whois.replaceAll("[<]{1}(a)[^>]+[>]{1}", "").replaceAll("<\\/a>", "");
				//whois = whois.replaceAll("(src=\")", "src=\"http://www.whoismind.com");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return whois;
	}
	
	public static TreeMap<String, String> getDns(String name) {
		String url = "http://www.domaintools.com/research/dns/?query=";
		
		TreeMap<String, String> dns = new TreeMap<String, String>();
		
		String dnsServer = "";
		String dnsRecord = "";
		try {
			
			//Get data
			Document doc = Jsoup.connect(url + name)
						.userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
						.get();
			
			//Get dnsRecord
			dnsRecord = doc.select("#main-body-content .main-left-content .wide-100 p").html();
			if (!dnsRecord.isEmpty()) {
				dnsRecord = dnsRecord.replaceAll("[<]{1}(a)[^>]+[>]{1}", "").replaceAll("<\\/a>", "");
				dns.put("dnsRecord", dnsRecord);
			}
			
			
			
		} catch (Exception e) {
			//e.printStackTrace();
		}
		
		return dns;
	}
	
	public static TreeMap<String, String> getIp(String name) {
		TreeMap<String, String> ipAddress = new TreeMap<String, String>();
		
		//Get ip address
		String urlIpAddress = "http://www.nearby.org.uk/api/dig.php?l=" + name + ".html";
		try {
			Document doc = Jsoup.connect(urlIpAddress).get();
			
			String ip = doc.text();
			
			ipAddress.put("ip", ip);
			
		} catch (Exception e) {
			//e.printStackTrace();
		}
		
		if (ipAddress.get("ip") != null && !ipAddress.get("ip").isEmpty()) {
			
			String urlInfoIp = "http://whatismyipaddress.com/ip/" + ipAddress.get("ip");
			
			try {
				Document doc = Jsoup.connect(urlInfoIp).get();
				
				String decimal = doc.select("#section_content table:eq(0) tr:eq(1) td").html();
				String hostname = doc.select("#section_content table:eq(0) tr:eq(2) td").html();
				String isp = doc.select("#section_content table:eq(0) tr:eq(3) td").html();
				String organization = doc.select("#section_content table:eq(0) tr:eq(4) td").html();
				String services = doc.select("#section_content table:eq(0) tr:eq(5) td").html();
				String type = doc.select("#section_content table:eq(0) tr:eq(6) td").html();
				type = type.replaceAll("[<]{1}(a)[^>]+[>]{1}", "").replaceAll("<\\/a>", "");
				
				String assignment = doc.select("#section_content table:eq(0) tr:eq(7) td").html();
				assignment = assignment.replaceAll("[<]{1}(a)[^>]+[>]{1}", "").replaceAll("<\\/a>", "");
				
				String country = doc.select("#section_content > table tr:eq(0) td").html();
				String region = doc.select("#section_content > table tr:eq(1) td").html();
				String city = doc.select("#section_content > table tr:eq(2) td").html();
				String latitude = doc.select("#section_content > table tr:eq(3) td").html();
				String longitude = doc.select("#section_content > table tr:eq(4) td").html();
				String areaCode = doc.select("#section_content > table tr:eq(5) td").html();
				String postalCode = doc.select("#section_content > table tr:eq(6) td").html();
				
				ipAddress.put("decimal", decimal);
				ipAddress.put("hostname", hostname);
				ipAddress.put("isp", isp);
				ipAddress.put("organization", organization);
				ipAddress.put("services", services);
				ipAddress.put("type", type);
				ipAddress.put("assignment", assignment);
				ipAddress.put("country", country);
				ipAddress.put("region", region);
				ipAddress.put("city", city);
				ipAddress.put("latitude", latitude);
				ipAddress.put("longitude", longitude);
				ipAddress.put("areaCode", areaCode);
				ipAddress.put("postalCode", postalCode);
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			
			
		}
		
		return ipAddress;
	}
	
	public static String getWhoisIp(String name) {
		
		String url = "http://www.who.is/whois-ip/ip-address/";
		
		String whoisIp = "";
		try {
			Document doc = Jsoup.connect(url + name).get();
			whoisIp = doc.select("#domain-data .tab-pane .domain-data pre").html();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return whoisIp;
	}
}
